package org.example.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;
import org.example.domain.Book;
import org.example.domain.User;
import org.example.domain.response.ResponseResult;

import java.util.List;

@Mapper
public interface UserDao extends BaseMapper<User> {
    @Insert("insert into user_role(user_id,role_id) values(#{userId},#{roleId})")
    public boolean insertUserRole(int userId,int roleId);

    @Update("update user_role set role_id = #{roleId} where user_id = #{userId}")
    public boolean updateUserRole(int userId,int roleId);

    @Select("select id from role where role_key = #{roleKey}")
    public int getRoleIdByRoleKey(String roleKey);

    @Select("select role_key from role,user_role where role.id=user_role.role_id and user_id=#{userId}")
    public String getRoleByUserId(int userId);

    @Select("select password from user where id = #{userId}")
    String getPasswordById(int userId);

    @Update("update user set password = #{password} where id = #{userId}")
    int updatePasswordById(int userId, String password);

    @Insert("insert into book_save values(null,#{userId},#{bookId})")
    int saveUserBook(int userId, int bookId);

    @Delete("delete from book_save where user_id = #{userId} and book_id = #{bookId}")
    int deleteUserBook(int userId, int bookId);

    @Select("select book_id from book_save where user_id = #{userId}")
    List<Integer> getUserSaveBooks(int userId);

    @Select("select count(*) from book_save where user_id = #{userId} and book_id = #{bookId}")
    int getUserBookById(int userId, int bookId);

    @Insert("insert into discussion_save values(null,#{userId},#{discussionId})")
    int saveUserDiscussion(int userId, int discussionId);

    @Delete("delete from discussion_save where user_id = #{userId} and discussion_id = #{discussionId}")
    int deleteUserDiscussion(int userId, int discussionId);

    @Select("select discussion_id from discussion_save where user_id = #{userId}")
    List<Integer> getUserSaveDiscussion(int userId);

    @Select("select count(*) from discussion_save where user_id = #{userId} and discussion_id = #{discussionId}")
    int getUserDiscussionById(int userId, int discussionId);

    @Select("select distinct type from discussion where id in (select discussion_id from discussion_save where user_id = #{userId})")
    List<String> getUserDiscussionTypeById(int userId);

    @Select("select * from user where username = #{username}")
    User getUserByUserName(String username);

    @Select("select icon from user where id = #{userId}")
    String getIcon(int userId);

    @Select("select username from user where id = #{id}")
    String getUserNameById(int id);
}
